#!/usr/bin/env node

const mysql = require('mysql2/promise'),
      log   = console.log;

//连接
let con = mysql.createPool({
  host:'localhost',
  user:'root',
  password:'ddd',
  database:'test'
});

//getAreas();

async function getAreas(){
  const sql = 'select * from areas';
  
 // let result = await con.execute(sql);//异步操作

 // log(result);
 
  let [rows] = await con.execute(sql);
  return rows;
  //exectue sql
}

//增


async function addArea(areaName){
  const sql = `insert into areas(area_name) values('${areaName}')`;
  let [rows] = await con.execute(sql);
  return rows;
}


//改
async function updateArea(oldName,newName){
  const sql = `update areas set area_name = '${newName}' where area_name = '${oldName}'`;
 
  let [rows] = await con.execute(sql);
  return rows;
}

//删

async function delArea(areaName){
  const sql = `delete from areas where area_name = '${areaName}'`;
  let [rows] = await con.execute(sql);
  return rows;
}

(async function() {
  //await addArea('英国');   //增
  //await updateArea('英国','大不列颠'); //改
  await delArea('大不列颠'); //删
  log(await getAreas());
})();//匿名函数
